import { AssetManager } from "cc";
import Singleton from "./Singleton";
import { assetManager } from "cc";
import { Sprite } from "cc";
import { SpriteFrame } from "cc";


export default class NGameRes{

    static bundle:AssetManager.Bundle;

    static async load(url:string){
        return new Promise(async resolve => {
            function loadRes(){
                NGameRes.bundle.load(url,resolve);
            }
            if(!this.bundle){
                assetManager.loadBundle('ngame_src', function (err, bundle) {
                    NGameRes.bundle = bundle;
                    loadRes();
                });
            }else{
                loadRes();
            }
        });
    }

    static async loadBundle():Promise<AssetManager.Bundle>{
        return new Promise(async resolve => {
            if(!this.bundle){
                assetManager.loadBundle('ngame_src', function (err, bundle) {
                    resolve(NGameRes.bundle = bundle);
                });
            }else{
                resolve(this.bundle)
            }
        });
    }

    static async BindSpriteFrame(sprite: Sprite, path: string){
        (await this.loadBundle()).load<SpriteFrame>(path,(err,data:SpriteFrame) => {
            sprite.spriteFrame = data;
        })
    }
    
}

